/*
 *
 * (c) Copyright Ascensio System Limited 2010-2018
 *
 * The MIT License (MIT)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */


package controllers;

import helpers.DocumentManager;
import helpers.ServiceConverter;

import java.io.*;
import java.net.URL;
import java.util.Scanner;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import entities.FileType;
import helpers.FileUtility;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import org.primeframework.jwt.domain.JWT;

@WebServlet(name = "PdfTestServlet", urlPatterns = {"/PdfTestServlet"})
@MultipartConfig
public class PdfTestServlet extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        File file = new File(DocumentManager.StoragePath("test.pdf", null));

        long contentLength=file.length();
        InputStream ins=null;

        response.setContentType("application/force-download");// 设置强制下载不打开

//火狐下载文件时文件名有空格会导致文件名显示不全,需要在名称加双引号
        response.addHeader("Content-Disposition", "attachment;fileName=\"" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1") + "\"");
//response.addHeader("Content-Length", Long.toString(contentLength));
        OutputStream os =null;
        byte[] buffer = new byte[1024];
//文件内容大于2KB则压缩
        if(contentLength > 2048){
            response.addHeader("Content-Encoding", "gzip");
            ins= new BufferedInputStream(new FileInputStream(file));
            os = new GZIPOutputStream(response.getOutputStream());
        }else{
            ins = new FileInputStream(file);
            os = response.getOutputStream();
        }
        int len = -1;
        while ((len = ins.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        os.flush();
        os.close();
        ins.close();
        ins=null;

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo()
    {
        return "Handler";
    }
}
